﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class FileDownloadDal : Base
    {
        public enum Fields
        {
            Id, 
		    FileId,
		    UserId,
		    IPAddress,
            Path,
            Name,
		    Created
        }

        protected override void ReadRecord(System.Collections.Hashtable table, System.Data.SqlClient.SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.FileId, reader, table);
            GetReaderValue<int?>(Fields.UserId, reader, table);
            GetReaderValue<string>(Fields.IPAddress, reader, table);
            GetReaderValue<string>(Fields.Path, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
        }

        public void Update(ref int id, int fileId, int? userId, string ipAddress, string path, string name, DateTime created, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "FileId", fileId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "UserId", userId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "IPAddress", ipAddress, System.Data.SqlDbType.VarChar, 15);
            AddInputParameter(cmd, "Path", path, System.Data.SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "Name", name, System.Data.SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "Created", created, System.Data.SqlDbType.DateTime);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, System.Data.SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", System.Data.SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", System.Data.SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }
    }
}
